<template>
  <div class="task-management">
    <div class="page-header">
      <h2>任务管理</h2>
      <el-button type="primary" @click="refreshTasks">刷新</el-button>
    </div>
    
    <!-- 任务列表 -->
    <el-card shadow="never" class="task-list-card">
      <el-table 
        :data="taskList" 
        style="width: 100%" 
        v-loading="loading"
        border
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="任务标题" min-width="200" />
        <el-table-column prop="companyName" label="发布企业" width="150" />
        <el-table-column prop="jobType" label="任务类型" width="120" />
        <el-table-column prop="location" label="工作地点" width="150" />
        <el-table-column prop="salary" label="报酬" width="100">
          <template #default="scope">
            ¥{{ scope.row.salary }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleViewTask(scope.row)"
            >
              查看
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleDeleteTask(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { getAllTasks, deleteTask } from '@/api/admin'

const router = useRouter()
const loading = ref(false)
const taskList = ref([])

const getStatusType = (status) => {
  switch (status) {
    case 'published': return 'primary'
    case 'assigned': return 'success'
    case 'pending_completion': return 'warning'
    case 'completed': return 'info'
    case 'cancelled': return 'danger'
    default: return 'info'
  }
}

const getStatusText = (status) => {
  switch (status) {
    case 'published': return '已发布'
    case 'assigned': return '进行中'
    case 'pending_completion': return '待确认'
    case 'completed': return '已完成'
    case 'cancelled': return '已取消'
    default: return '未知状态'
  }
}

const formatDate = (date) => {
  if (!date) return '-'
  const d = new Date(date)
  return d.toLocaleString()
}

const loadTasks = async () => {
  loading.value = true
  try {
    const response = await getAllTasks()
    if (response && response.data) {
      taskList.value = response.data
    } else {
      taskList.value = []
    }
  } catch (error) {
    console.error('获取任务列表失败:', error)
    ElMessage.error('获取任务列表失败')
  } finally {
    loading.value = false
  }
}

const refreshTasks = () => {
  loadTasks()
}

const handleViewTask = (task) => {
  router.push(`/task/${task.id}`)
}

const handleDeleteTask = (task) => {
  ElMessageBox.confirm(
    `确定要删除任务 "${task.title}" 吗？此操作不可逆！`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(async () => {
      try {
        const response = await deleteTask(task.id)
        if (response && response.data) {
          ElMessage.success('删除任务成功')
          loadTasks() // 重新加载任务列表
        } else {
          ElMessage.error('删除任务失败')
        }
      } catch (error) {
        console.error('删除任务失败:', error)
        ElMessage.error(error.message || '删除任务失败')
      }
    })
    .catch(() => {
      // 用户取消删除操作
    })
}

onMounted(() => {
  loadTasks()
})
</script>

<style scoped>
.task-management {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
}

.task-list-card {
  margin-bottom: 20px;
}
</style> 